{
  LX Tableau module

  Based on modo's SDK lxtableau.h which is:

  Copyright (c) 2008-2012 Luxology LLC

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.   Except as contained
  in this notice, the name(s) of the above copyright holders shall not be
  used in advertising or otherwise to promote the sale, use or other dealings
  in this Software without prior written authorization.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.
}

{
  Copyright (c) 2012 David Ballesteros and Jameel Halabi

  This unit is part of the BLAISEmodo wrapper,
  which is licensed under the BSD 2-Clause license (see licenses/license.txt).
}


unit Lx_Tableau;

{$mode objfpc}{$H+}

interface

uses
  Lx_Result, Lx_VMath, Lx_Vector;

const
  LXa_TABLEAUELEMENT              = 'tableauElement' ;
  LXa_TABLEAUSURFACE              = 'tableauSurface' ;
  LXa_TABLEAUVOLUME               = 'tableauVolume'  ;
  LXa_TABLEAULIGHT                = 'tableauLight'   ;
  LXa_TABLEAUPROXY                = 'tableauProxy'   ;
  LXa_TABLEAUINSTANCE             = 'tableauInstance';
  LXa_TABLEAUSHADER               = 'tableauShader'  ;
  LXa_TABLEAUFILTER               = 'tableauFilter'  ;

  LXfTBLX_VOL_VOLUME              = 1;
  LXfTBLX_VOL_CLIPPING            = 2;
  LXfTBLX_VOL_IMPSURF             = 4;

  LXfTBLX_FILT_PIXEL              = $01;
  LXfTBLX_FILT_STRIP              = $02;
  LXfTBLX_FILT_RELATIVE           = $04;

  LXfTBLX_PREVIEW_UPDATE_NONE     = $00;
  LXfTBLX_PREVIEW_UPDATE_GEOMETRY = $01;
  LXfTBLX_PREVIEW_UPDATE_SHADING  = $02;

  LXiTBLX_SEG_POINT               = 1;
  LXiTBLX_SEG_LINE                = 2;
  LXiTBLX_SEG_TRIANGLE            = 3;

  LXiTBLX_CONNECT_QUAD            = 0;
  LXiTBLX_CONNECT_BEGIN           = 1;
  LXiTBLX_CONNECT_END             = 2;

  LXiTBLX_ENDPOINT_T0             = 0;
  LXiTBLX_ENDPOINT_T1             = 1;

  LXsTBLX_FEATURE_POS             = 'pos' ;
  LXsTBLX_FEATURE_OBJPOS          = 'opos';
  LXsTBLX_FEATURE_VEL             = 'vel' ;
  LXsTBLX_FEATURE_NORMAL          = 'norm';
  LXsTBLX_FEATURE_RADIUS          = 'rad' ;
  LXsTBLX_FEATURE_PARAM_LENGTH    = 'plen';

  LXsTBLX_PARTICLE_POS            = 'pos'       ;
  LXsTBLX_PARTICLE_XFRM           = 'xfrm'      ;
  LXsTBLX_PARTICLE_ID             = 'id'        ;
  LXsTBLX_PARTICLE_SIZE           = 'size'      ;
  LXsTBLX_PARTICLE_VEL            = 'vel'       ;
  LXsTBLX_PARTICLE_MASS           = 'mass'      ;
  LXsTBLX_PARTICLE_FORCE          = 'force'     ;
  LXsTBLX_PARTICLE_AGE            = 'age'       ;
  LXsTBLX_PARTICLE_PATH           = 'pathLength';
  LXsTBLX_PARTICLE_DISS           = 'dissolve'  ;

  { Added in SDK 52162 }
  LXsP_LGT_QUALITY                = 'light.quality'  ;
  LXsP_LGT_SHADOWING              = 'light.shadowing';
  LXsP_LGT_SOURCE                 = 'light.source'   ;
  LXsP_LGT_COLOR                  = 'light.color'    ;
  { end Added in SDK 52162}

  LXs_FX_LIGHTCOLOR               = 'lightColor'        ;
  LXs_FX_LIGHTSHADOW              = 'lightShadow'       ;
  LXs_FX_LIGHTDIFFUSE             = 'lightDiffuse'      ;
  LXs_FX_LIGHTSPECULAR            = 'lightSpecular'     ;
  LXs_FX_LIGHTCAUSTICS            = 'lightCaustics'     ;
  LXs_FX_LIGHTDISTANCE            = 'lightDistance'     ;
  LXs_FX_LIGHTANGLE               = 'lightAngle'        ;

  LXs_FX_VOLSCATTER               = 'volScattering'     ;
  LXs_FX_VOLSCATTER_COL           = 'volScatteringColor';
  LXs_FX_VOLABSORB                = 'volAbsorption'     ;
  LXs_FX_VOLABSORB_COL            = 'volAbsorptionColor';
  LXs_FX_VOLDENSITY               = 'volDensity'        ;
  LXs_FX_VOLLEVEL                 = 'volLevel'          ;
  LXs_FX_VOLSMPDENSITY            = 'volSampleDensity'  ;
  LXs_FX_VOLAMB_COL               = 'volAmbientColor'   ; // Added in SDK 52162

  LXsP_CAM_RAY                    = 'camera.ray'   ;
  LXsP_CAM_RAY_T1                 = 'camera.ray_T1';

  LXsP_ENV_INFINITECOLOR          = 'environment.color'     ;
  LXsP_ENV_VISIBILITY             = 'environment.visibility';

  LXsP_GLO_LIGHTING               = 'global.lighting'    ;
  LXsP_GLO_DISPLACEMENT           = 'global.displacement';
  LXsP_GLO_INDIRECT               = 'global.indirect'    ;
  LXsP_GLO_CAUSTICS               = 'global.caustics'    ;
  LXsP_GLO_RENDERING              = 'global.rendering'   ;

  LXsP_CAM_RESOLUTION             = 'camera.resolution';
  LXsP_CAM_FRUSTUM                = 'camera.frustum'   ;
  LXsP_CAM_FRUSTUM_T1             = 'camera.frustum_T1';
  LXsP_CAM_PIXEL                  = 'camera.pixel'     ;
  LXsP_CAM_UVBAKE                 = 'camera.uvBake'    ;

  LXu_TABLEAU                     = '{76C4EDD9-5FF9-4342-BB08-EFCD0B344004}';
  LXu_TABLEAUELEMENT              = '{71D90AD9-3E30-4CE8-9E2B-F70DA281B2DC}';
  LXu_TABLEAUSURFACE              = '{B5769A8B-6254-42E3-862F-0A811E716E21}';
  LXu_TRIANGLESOUP                = '{17536C2D-07BD-49C8-8348-CEBDE4D3A856}';
  LXu_TABLEAUVOLUME               = '{97962302-4B49-4282-B259-F347F1012818}';
  LXu_TABLEAULIGHT                = '{7FE816D1-4A7F-4BE5-9689-4991C03CAEE0}';
  LXu_LIGHTSAMPLE                 = '{43734621-9B93-4174-AC63-E2FE7DDA8794}';
  LXu_TABLEAUPROXY                = '{FB34BD64-099C-4B30-8EF7-2BB04CA0E92C}';
  LXu_TABLEAUINSTANCE             = '{9B57D0DB-C0B4-435E-8CDD-E3DDCF66172D}';
  LXu_TABLEAUSHADER               = '{A0E3B574-B0AC-4530-A43F-7CC8DA536E25}';
  LXu_SHADERSLICE                 = '{47B885B9-B1D9-4F86-829F-A6AABBD7FFF7}';
  LXu_TABLEAUVERTEX               = '{F90A0A39-EE2A-4D11-912B-9338EF271DFF}';
  LXu_TABLEAUFILTER               = '{FD1326E7-EECF-4EAC-B9AD-B1D64E7503BE}';
  LXu_TABLEAUSOURCE               = '{1121C167-F934-4421-8ABE-32F8E4659324}';
  LXu_TABLEAUSERVICE              = '{8DF92316-3172-465A-A199-254792D37732}';

type
  LXtTableauBox    = array [0..5] of Single;

  PLXpLightQuality = ^LXpLightQuality;
  LXpLightQuality  = record
    Center   : LXtFVector;
    Radius   ,
    Power    : Single    ;
    Infinite ,
    Indirect ,
    ShadType ,
    Samples  ,
    Fast     : UInt32    ;
    Item     : Pointer   ;
  end;

  PLXpLightShadowing = ^LXpLightShadowing;
  LXpLightShadowing  = record
    Pos     ,
    Ray, Up : LXtFVector;
    Angle   : Single    ;
    MapRes  : UInt32    ;
    Exclude : Pointer   ;
  end;

  PLXpLightSource = ^LXpLightSource;
  LXpLightSource  = record
    WPos, OPos ,
    Dir        : LXtFVector;
    Len        : Single;
  end;

  PLXpLightColor = ^LXpLightColor;
  LXpLightColor  = record
    Color      ,
    Shadow     ,
    UnShad     : LXtFVector;
    Diffuse    ,
    Specular   ,
    Caustics   ,
    SubSurface ,
    Brightness : Single    ;
  end;

  PLXpCameraRay = ^LXpCameraRay;
  LXpCameraRay  = record
    Dir    ,
    Origin : LXtFVector;
    Spread : Single    ;
  end;

  PLXpEnvInfiniteColor = ^LXpEnvInfiniteColor;
  LXpEnvInfiniteColor  = record
    RGB       : LXtFVector;
    Intensity : Single    ;
  end;

  PLXpEnvVisibility = ^LXpEnvVisibility;
  LXpEnvVisibility  = record
    Camera     ,
    Indirect   ,
    Reflection ,
    Refraction : UInt32;
  end;

  PLXpGlobalLighting = ^LXpGlobalLighting;
  LXpGlobalLighting  = record
    AmbientColor     : LXtFVector;
    AmbientIntensity ,
    RayThresh        : Single    ;
    ShadDepth        ,
    ReflDepth        ,
    RefrDepth        ,
    Unbiased         ,
    SpecSmps         ,
    AnimNoise        ,
    RayAccel         ,
    BatchSize        : UInt32    ;
  end;

  PLXpGlobalDisplacement = ^LXpGlobalDisplacement;
  LXpGlobalDisplacement  = record
    Rate    ,
    Ratio   ,
    Jitter  ,
    EdgeMin : Single;
    Enable  ,
    Smooth  ,
    UseBump : UInt32;
  end;

  PLXpGlobalIndirect = ^LXpGlobalIndirect;
  LXpGlobalIndirect  = record
    Range      ,
    IrrRays2   ,
    IrrRate    ,
    IrrRatio   ,
    IrrSmooth  : Single;
    Enable     ,
    Scope      ,
    Limit      ,
    Rays       ,
    Subs       ,
    Vols       ,
    Bump       ,
    Super      ,
    Reject     ,
    Caus       ,
    GIBack     ,
    IrrCache   ,
    IrrDirect2 ,
    IrrRays    ,
    IrrVals    ,
    IrrGrads   ,
    IrrStart   ,
    IrrEnd     ,
    IrrWalk    ,
    IrrLEnable ,
    IrrSEnable ,
    RadCache   : UInt32;
    IrrLName   ,
    IrrSName   : PChar ;
  end;

  PLXpGlobalCaustics = ^LXpGlobalCaustics;
  LXpGlobalCaustics  = record
    Multiplier : Single;
    Total      ,
    Local      ,
    Walk       : UInt32;
  end;

  PLXpGlobalRendering = ^LXpGlobalRendering;
  LXpGlobalRendering  = record
    SubDRate        ,
    CoarseRate      ,
    FineRate        ,
    FineThresh      ,
    AAImpMin        : Single;
    MultiGeo        ,
    SubDAdapt       ,
    RenderType      ,
    AA              ,
    AAFilter        ,
    MotionBlur      ,
    Field           ,
    FrmPasses       ,
    DOF             ,
    Stereo          ,
    StereoEye       ,
    StereoComposite ,
    UpAxis          ,
    BucketX         ,
    BucketY         ,
    BktOrder        ,
    BktReverse      ,
    BktWrite        ,
    BktSkip         ,
    BktRefine       ,
    ARefine         ,
    MergeRad        ,
    FullBuff        : UInt32;
  end;

  PLXpCameraResolution = ^LXpCameraResolution;
  LXpCameraResolution  = record
    Width                      ,
    Height                     : UInt32;
    PixelAspect                ,
    DPI                        ,
    RegX0, RegX1, RegY0, RegY1 : Single;
  end;

  PLXpCameraFrustum = ^LXpCameraFrustum;
  LXpCameraFrustum  = record
    EyePos                     : LXtFVector;
    Xfrm, InvXfrm              : LXtMatrix ;
    FocalLength                ,
    FocusDist, FStop           ,
    IrisRot, IrisBias, Distort ,
    IoDist, ConvDist           ,
    BlurLength, BlurOffset     ,
    ApertureX, ApertureY       ,
    OffsetX, OffsetY           ,
    Squeeze                    ,
    Target                     ,
    ClipDist                   : Single    ;
    Clipping                   ,
    FilmFit                    ,
    ProjType                   ,
    IrisBlades                 : Integer   ;
    Item                       : Pointer   ;
  end;

  PLXpCameraPixel = ^LXpCameraPixel;
  LXpCameraPixel  = record
    X, Y : Single;
  end;

  PLXpCameraUVBake = ^LXpCameraUVBake;
  LXpCameraUVBake  = record
    Name : PChar;
  end;

  ILxTableau = interface
  [LXu_TABLEAU]
    function Time        (    T0       : PSingle ;
		              Interval : PSingle ) : LxResult;
    function AddElement  (    Element  : IUnknown;
                              Shader   : IUnknown) : LxResult;
    function AddInstance (    Inst     : IUnknown;
                              Element  : IUnknown;
			      Shader   : IUnknown) : LxResult;
    function Channels    (    &Type    : UInt32  ;
                          out VObj               ) : LxResult;
    function Visible     (    Item     : IUnknown) : LxResult;
    function FindShader  (    Item     : IUnknown;
                              Tags     : IUnknown;
                          out VObj               ) : LxResult;
    function InstanceItem                          : IUnknown;
  end;

  ILxTableauElement = interface
  [LXu_TABLEAUELEMENT]
    function Bound         (BBox  : LXtTableauBox) : LxResult;
    function FeatureCount  (&Type : LXtID4       ) : UInt32  ;
    function FeatureByIndex(&Type : LXtID4       ;
                            Index : UInt32       ) : LxResult;
    function SetVertex     (VDesc : IUnknown     ) : LxResult;
  end;

  ILxTableauSurface = interface
  [LXu_TABLEAUSURFACE]
    function Bound         (      BBox    : LXtTableauBox) : LxResult;
    function FeatureCount  (      &Type   : LXtID4       ) : UInt32  ;
    function FeatureByIndex(      &Type   : LXtID4       ;
                                  Index   : UInt32       ;
                                  Name    : PPChar       ) : LxResult;
    function SetVertex     (      VDesc   : IUnknown     ) : LxResult;
    function Sample        (const BBox    : LXtTableauBox;
                                  Scale   : Single       ;
				  TriSoup : IUnknown     ) : LxResult;
    function Padding       (      Dist    : PSingle      ) : LxResult;
  end;

  ILxTriangleSoup = interface
  [LXu_TRIANGLESOUP]
    function TestBox(const BBox   : LXtTableauBox) : UInt32  ;
    function Segment(      SegID  : UInt32       ;
                           &Type  : UInt32       ) : LxResult;
    function Vertex (const Vertex : PSingle      ;
                           Index  : PUInt32      ) : LxResult;
    function Polygon(      V0     : UInt32       ;
                           V1     : UInt32       ;
                           V2     : UInt32       ) : LxResult;
    function Connect(      &Type  : Uint32       ) : LxResult;
  end;

  ILxTableauVolume = interface
  [LXu_TABLEAUVOLUME]
    function Bound         (      BBox         : LXtTableauBox) : LxResult;
    function FeatureCount  (      &Type        : LXtID4       ) : UInt32  ;
    function FeatureByIndex(      &Type        : LXtID4       ;
                                  Index        : UInt32       ;
                            const Name         : PPChar       ) : LxResult;
    function SetVertex     (      VDesc        : IUnknown     ) : LxResult;
    function &Type                                              : Integer ;
    function RenderInit    (      SV           : IUnknown     ) : LxResult;
    function RaySample     (      DensitySlice : IUnknown     ;
                                  ShadingSlice : IUnknown     ;
                                  SV           : IUnknown     ;
                                  RayCastObj   : IUnknown     ;
                                  RayMarchObj  : IUnknown     ) : LxResult;
    function RayCast       (      DensitySlice : IUnknown     ;
                                  SV           : IUnknown     ;
                                  RayCastObj   : IUnknown     ;
                                  Dist         : PDouble      ) : LxResult;
    function Density       (      DensitySlice : IUnknown     ;
                                  SV           : IUnknown     ;
                                  RayCastObj   : IUnknown     ;
                            const Pos          : LXtVector    ;
                                  WorldPos     : Integer      ;
                                  Dens         : PDouble      ) : LxResult;
  end;

  ILxTableauLight = interface
  [LXu_TABLEAULIGHT]
    function Bound         (      BBox     : LXtTableauBox   ) : LxResult;
    function FeatureCount  (      &Type    : LXtID4          ) : UInt32  ;
    function FeatureByIndex(      &Type    : LXtID4          ;
                                  Index    : UInt32          ;
                            const Name     : PPChar          ) : LxResult;
    function SetVertex     (      VDesc    : IUnknown        ) : LxResult;
    function Sample        (      Sample   : Integer         ;
                                  JU       : Single          ;
                                  JV       : Single          ;
                            const Dir      : LXtFVector      ;
                                  WPos     : LXtFVector      ;
                                  OPos     : LXtFVector      ) : LxResult;
    function Geometry      (      GC       : Pointer         ) : Integer ;
    function Emit          (      NPhotons : UInt32          ;
                                  Offset   : Integer         ;
                                  Slice    : IUnknown        ;
                                  SV       : PLXtSampleVector) : LxResult;
    function ShadowMap     (  out VObj                       ) : Integer ;
  end;

  ILxLightSample = interface
  [LXu_LIGHTSAMPLE]
    procedure Vertex(const Vertex : PSingle);
  end;

  ILxTableauProxy = interface
  [LXu_TABLEAUPROXY]
    function Bound         (      BBox    : LXtTableauBox) : LxResult;
    function FeatureCount  (      &Type   : LXtID4       ) : UInt32  ;
    function FeatureByIndex(      &Type   : LXtID4       ;
                                  Index   : UInt32       ;
                            const Name    : PPChar       ) : LxResult;
    function SetVertex     (      VDesc   : IUnknown     ) : LxResult;
    function Sample        (      BBox    : LXtTableauBox;
                                  Tableau : IUnknown     ) : LxResult;
  end;

  ILxTableauInstance = interface
  [LXu_TABLEAUINSTANCE]
    function Properties  (VecStack : IUnknown ) : LxResult;
    function GetTransform(EndPoint : UInt32   ;
                          Offset   : LXtVector;
                          Xfrm     : LXtMatrix) : LxResult;
  end;

  ILxTableauShader = interface
  [LXu_TABLEAUSHADER]
    function Select(    TEElt    : IUnknown;
                        TVDesc   : IUnknown) : LxResult;
    function Slice (    VTOutput : IUnknown;
                        TVDesc   : IUnknown;
                    out VObj               ) : LxResult;
  end;

  ILxShaderSlice = interface
  [LXu_SHADERSLICE]
    function Evaluate(VecStack : IUnknown) : LxResult;
  end;

  ILxTableauVertex = interface
  [LXu_TABLEAUVERTEX]
    function AddFeature(      &Type  : LXtID4 ;
                        const Name   : PChar  ;
                              Index  : PUInt32) : LxResult;
    function Lookup    (      &Type  : LXtID4 ;
                        const Name   : PChar  ;
                              Offset : PUInt32) : LxResult;
    function Size                               : Uint32  ;
    function Count                              : Uint32  ;
    function ByIndex   (      Index  : UInt32 ;
                              &Type  : LXtID4 ;
                        const Name   : PChar  ;
                              Offset : UInt32 ) : LxResult;
  end;

  ILxTableauFilter = interface
  [LXu_TABLEAUFILTER]
    function Order                                    : PChar   ;
    function Select(      Framebuf : IUnknown       ) : LxResult;
    function Bound (      Box      : array of Single;              // Box [0..3]
                          Kernel   : PSingle        ) : UInt32  ;
    function Pixel (      Framebuf : IUnknown       ;
                    const Pos      : array of Single) : UInt32  ;  // Pos [0..1]
    function Region(      Framebuf : IUnknown       ;
                          Box      : array of Single) : UInt32  ;  // Box [0..3]
  end;

  ILxTableauSource = interface
  [LXu_TABLEAUSOURCE]
    function Elements     (    Tableau   : IUnknown) : LxResult;
    function Preview      (    Tableau   : IUnknown) : LxResult;
    function Instance     (    Tableau   : IUnknown;
                               Instance  : IUnknown) : LxResult;
    function SubShader    (    Tableau   : IUnknown;
                           out VObj                ) : LxResult;
    function PreviewUpdate(    ChanIndex : Integer ;
                               Update    : PInteger) : LxResult;
    function GetCurves    (    Tableau   : IUnknown;
                               Tags      : IUnknown;
                           out VObj                ) : LxResult;
  end;

  ILxTableauService = interface
  [LXu_TABLEAUSERVICE]
    function ScriptQuery(out VObj) : LxResult;
    function AllocVertex(out VObj) : LxResult;
  end;

function LXe_INFINITE_BOUND   : LxResult;
function LXiTBLX_BASEFEATURE  : UInt32  ;
function LXiTBLX_DPDU         : LXtID4  ;
function LXiTBLX_PARTICLES    : LXtID4  ;
function LXeTBLXSURF_ADAPTIVE : LxResult;

implementation

function LXe_INFINITE_BOUND   : LxResult; inline;
begin
  Result := LXxGOODCODE(LXeSYS_REND, 1);
end;
function LXiTBLX_BASEFEATURE  : LXtID4  ; inline;
begin
  Result := LXxID4(' ','v','r','t');
end;
function LXiTBLX_DPDU         : LXtID4  ; inline;
begin
  Result := LXxID4('d','p','d','u');
end;
function LXiTBLX_PARTICLES    : LXtID4  ; inline;
begin
  Result := LXxID4('p','r','t','i');
end;
function LXeTBLXSURF_ADAPTIVE : LxResult; inline;
begin
  Result := LXxGOODCODE(LXeSYS_REND, 2);
end;

end.

